package cn.diancan.action.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.SpecificationValue;
import cn.diancan.vo.manager.ProductManagerActionVo;
import cn.diancan.vo.model.ProductVo;
import cn.diancan.vo.model.SpecificationVo;
import cn.diancan.vo.other.Pager;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends BaseAction implements ModelDriven<ProductManagerActionVo> {

    private ProductManagerActionVo requestVo = new ProductManagerActionVo();
    private Pager pager = new Pager();

    //List
    @ManagerAuth
    public String productListPage(){
	try {
	    pager.setPageNumber(requestVo.getPageNumber());
	    pager.setPageSize(10);
	    
	    productService.findProductVoPager(pager);
	    
	    /*this.put("applySexTypeEnum", ApplySexType.values());
	    this.put("pager", pager);*/
	    this.put("requestVo", requestVo);

	    return freemarkView("/views/ftl/manager/product/product_list.ftl");
	} catch (OutMsgException e) { 
	    return managerError(e.getMessage());
	}
    }
    
    @ManagerAuth
    public String productListByGoods() {
	try {
	    //List<Product> list = productService.findProductListByGoodsId(requestVo.getGoodsId());
	    
	    List<ProductVo> list = productService.findProductVoListByGoodsId(requestVo.getGoodsId());
	    return jsonFormat(list);
	} catch (OutMsgException e) {
	    return jsonError(e);
	}
    }
    
    @ManagerAuth
    public String saveProducts(){
	try {
	    Boolean result = productService.saveColorAndSizeProductList(requestVo.getGoodsId(), requestVo.getSizeSpecificationValueIds(), requestVo.getColorSpecificationValueIds());
	    return jsonFormat(result);
	} catch (OutMsgException e) { 
	    return jsonError(e);
	}catch (Exception e) {
	    return jsonError("系统繁忙");
	}
    }
    
    
    
    
    
    //根据条码获取商品
    @ManagerAuth
    public String productByBarCode(){
	ProductVo productVo = productService.findVoByBarcode(requestVo.getBarCode());
	return jsonFormat(productVo);
    }
    
    
    @ManagerAuth
    public String productByAll(){ 
	try {
	    ProductVo productVo = productService.findProductVo(requestVo.getGoodsNumber(), requestVo.getColorSpecificationValueIds()[0], requestVo.getColorSpecificationValueIds()[0]);
	    return jsonFormat(productVo);
	} catch (Exception e) { 
	    return jsonError("系统错误");
	} 
    }
    
    //根据货号查找到颜色尺码
    @ManagerAuth
    public String specificationValueByGoodsNumber(){
	List<SpecificationVo> list  = productService.findColorSpecificationValueByGoodsNumber(requestVo.getGoodsNumber());
	List<SpecificationVo> list2 = productService.findSizeSpecificationValueByGoodsNumber(requestVo.getGoodsNumber());
	if(null==list || null==list2){
	    return jsonError("查找不到尺码或颜色");
	}
	
	Map<String,List<SpecificationVo>> map =new HashMap<String,List<SpecificationVo>>();
	map.put("color", list);
	map.put("size", list2); 
	return jsonFormat(map);
    }
    
    

    public Pager getPager() {
        return pager;
    }

    public void setPager(Pager pager) {
        this.pager = pager;
    }

    @Override
    public ProductManagerActionVo getModel() {
	return requestVo;
    }

}
